package ncc.sso.bs;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.bs.logging.Logger;
import nc.bcmanage.bs.IBusiCenterManageService;
import nc.bcmanage.vo.BusiCenterVO;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.BeanProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.login.bs.INCUserQueryService;
import nc.vo.pub.BusinessException;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFDateTime;
import nc.vo.sm.UserVO;
import ncc.sso.vo.NCCSSORegInfoVO;

public class PUMASSOImpl implements IPUMASSO {

	@Override
	public void pumaLoginOutRegister(String code) throws BusinessException {

	}

	@Override
	public String pumaLoginOutFetchToken(String code) throws BusinessException {
		BaseDAO Dao = new BaseDAO();
		String sql = "select puma_token from pub_nccsso where  user_code='" + code + "' order by ts desc";
		String token  = (String) Dao.executeQuery(sql, new ColumnProcessor());
		return token;
	}
	

	@Override
	public void pumaLoginRegister(String code,String puma_token,String token) throws BusinessException {
		NCCSSORegInfoVO vo = new NCCSSORegInfoVO();
		vo.setToken(token);
		vo.setUser_code(code);
		vo.setPuma_token(puma_token);
		int timeOut = 1200000;
		long expiredTime = System.currentTimeMillis() + timeOut;
		vo.setExpiredTime(new UFDateTime(expiredTime));
		new BaseDAO().insertVO(vo);
	}

	@Override
	public NCCSSORegInfo pumaLoginFetch(String token) throws BusinessException {
		
		BaseDAO Dao = new BaseDAO();
		UFDateTime time = new UFDateTime();
		String sql = "select * from pub_nccsso where dr =0 and  token='" + token + "' and expiredTime>='" + time + "' order by ts desc";
		NCCSSORegInfoVO reginfoVO =  null;
		try{
			reginfoVO = (NCCSSORegInfoVO) Dao.executeQuery(sql, new BeanProcessor(NCCSSORegInfoVO.class));   //(NCCSSORegInfoVO) NCLocator.getInstance().lookup(IUAPQueryBS.class).executeQuery(sql, new BeanProcessor(NCCSSORegInfoVO.class));
		}catch(Exception e) {
			e.printStackTrace();
		}
				
		if (reginfoVO != null) {
			NCCSSORegInfo ssoInfo = new NCCSSORegInfo();
			ssoInfo.setUsercode(reginfoVO.getUser_code());
			ssoInfo.setLangCode("simpchn");
			ssoInfo.setBusiCenterCode(null);
			ssoInfo.setGroupCode(null);
			ssoInfo.setAccess_token(reginfoVO.getToken());
			Map<BusiCenterVO, UserVO> availableBCs = getAvailableBusiCenters(ssoInfo);
			ssoInfo.putAttachProp("availableBCs", availableBCs);
			
			reginfoVO.setStatus(VOStatus.UPDATED); 
			reginfoVO.setDr(1);
			Dao.updateVO(reginfoVO);
			return ssoInfo;
		}
		return null;
	}

	private Map<BusiCenterVO, UserVO> getAvailableBusiCenters(NCCSSORegInfo regInfo) {
		String regBSCode = regInfo.getBusiCenterCode();
		String userCode = regInfo.getUsercode();
		Map map = new HashMap();
		INCUserQueryService service = (INCUserQueryService) NCLocator.getInstance().lookup(INCUserQueryService.class);
		int count = getBusiCenterList().size();
		for (int i = 0; i < count; i++) {
			BusiCenterVO bcvo = (BusiCenterVO) getBusiCenterList().get(i);
			String bsCode = bcvo.getCode();
			if ((regBSCode == null) || (regBSCode.equals(bsCode))) {
				String dsName = bcvo.getDataSourceName();
				try {
					UserVO user = service.findUserVO(dsName, userCode);
					if (user != null)
						map.put(bcvo, user);
				} catch (Exception e) {
					Logger.error(e.getMessage(), e);
				}
			}
		}
		return map;
	}

	private List<BusiCenterVO> getBusiCenterList() {
		List<BusiCenterVO> bcList = null;
		IBusiCenterManageService service = (IBusiCenterManageService) NCLocator.getInstance()
				.lookup(IBusiCenterManageService.class);
		try {
			BusiCenterVO[] bcVOs = service.getBusiCenterVOs();
			bcList = new ArrayList(Arrays.asList(bcVOs));
		} catch (Exception e) {
			Logger.error(e.getMessage(), e);
		}

		return bcList;
	}

}
